<%@ page contentType="text/html" %>
<%@ taglib prefix="jbfilter" uri="http://jbfilter.org/tag" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="datePattern1" value="dd/MM/yyyy"/>
<c:set var="datePattern2" value="yyyy"/>
<c:set var="countryClass" value="org.jbfilter.test.beans.Country"/>
<jsp:useBean id="hibernateSessionFactoryRetriever" class="org.jbfilter.demo.HibernateSessionFactoryRetrieverBean" scope="application"/>

<html>
<head>
<title>Intro jbfilter-hibernate</title>
</head>

<body>

<jbfilter:filter id="myIntroFilterHib" varComponents="comps" providerName="hibernate" clean="${param.clean}">

	<jbfilter:comparableComponent
		id="id"
		converter="java.lang.Long"
		comparatorKeyword="${param.id_keyword}"
		value="${param.id_value}"/>

	<jbfilter:dynamicLogicComponent id="logicName" logic="or" inverted="${param.name_inverted}">
		<jbfilter:containsComponent
			id="firstName"
			caseSensitive="${param.name_caseSensitive}"
			var="fName"
			value="${param.name_value}"/>
		<jbfilter:containsComponent
			id="lastName"
			caseSensitive="${param.name_caseSensitive}"
			var="lName"
			value="${param.name_value}"/>
	</jbfilter:dynamicLogicComponent>

	<jbfilter:startEndDateComponent
		id="born"
		inverted="${param.born_inverted}"
		startDate="${param.born_start}"
		endDate="${param.born_end}"
		varStartString="bornStart"
		varEndString="bornEnd"
		pattern="${datePattern1}" />

	<jbfilter:startEndDateComponent
		id="century"
		propertyPath="born"
		inverted="${param.century_inverted}"
		startDate="${empty (param.century_value) ? '' : fn:substring(param.century_value, 0 , 4)}"
		endDate="${empty (param.century_value) ? '' : fn:substring(param.century_value, 4 , 8)}"
		varEndString="centuryEnd"
		pattern="${datePattern2}" />

	<jbfilter:dateComponent
		id="died"
		value="${param.died}"
		varValueString="died"
		truncate="YEAR"
		pattern="yyyy" />
		
	<jbfilter:equalsComponent
		id="country"
		inverted="${param.country_inverted}"
		converter="${countryClass}"
		value="${param.country_value}"/>
		
</jbfilter:filter>	

<form action="">
	<table>
		<tr>
			<td>Id </td>
			<td>
				<select name="id_keyword">
					<option value="gt"<c:if test="${comps.id.comparatorKeyword == 'gt'}"> selected="selected"</c:if>>&gt;</option>
					<option value="ge"<c:if test="${comps.id.comparatorKeyword == 'ge'}"> selected="selected"</c:if>>&gt;=</option>
					<option value="eq"<c:if test="${comps.id.comparatorKeyword == 'eq'}"> selected="selected"</c:if>>=</option>
					<option value="lt"<c:if test="${comps.id.comparatorKeyword == 'lt'}"> selected="selected"</c:if>>&lt;</option>
					<option value="le"<c:if test="${comps.id.comparatorKeyword == 'le'}"> selected="selected"</c:if>>&lt;=</option>
				</select>
				<input type="text" name="id_value" value="${comps.id.value}"/>
			</td>
		</tr>	
		<tr>
			<td>Name </td>
			<td>
				<input type="text" name="name_value" value="${fName.value}"/>
				<input type="checkbox" name="name_caseSensitive" value="true" <c:if test="${fName.caseSensitive == 'true'}">checked="checked"</c:if>/>case sensitive
				<input type="checkbox" name="name_inverted" value="true" <c:if test="${comps.logicName.inverted == 'true'}">checked="checked"</c:if>/>Not
			</td>
		</tr>	
		<tr>
			<td>Born (${datePattern1})</td>
			<td>
				<input type="text" name="born_start" value="${bornStart}"/>
				(From)
				<input type="text" name="born_end" value="${bornEnd}"/>
				(To)
				<input type="checkbox" name="born_inverted" value="true" <c:if test="${comps.born.inverted == 'true'}">checked="checked"</c:if>/>Not
			</td>
		</tr>
		<tr>
			<td>Died (year) </td>
			<td>
				<input type="text" name="died" value="${died}"/>
			</td>
		</tr>
		<tr>
			<td>Century </td>
			<td>
				<select name="century_value">
					<option value="">&lt;All&gt;</option>
				<c:forEach begin="1700" end="2000" var="curr" step="100">
					<option value="${curr-100}${curr}"<c:if test="${curr == centuryEnd}"> selected="selected"</c:if>>${fn:substring(curr,0,2)}th</option>
				</c:forEach>			
				</select>
				<input type="checkbox" name="century_inverted" value="true" <c:if test="${comps.century.inverted == 'true'}">checked="checked"</c:if>/>Not
			</td>
		</tr>
		<tr>
			<td>Country </td>
			<td>
				<select name="country_value">
					<option value="">&lt;All&gt;</option>
					<jbfilter:forEachEnum enumClass="${countryClass}" var="curr">
						<option value="${curr}"<c:if test="${comps.country.value == curr}">selected="selected"</c:if>>${curr.displayName}</option>
					</jbfilter:forEachEnum>
				</select>
				<input type="checkbox" name="country_inverted" value="true" <c:if test="${comps.country.inverted == 'true'}">checked="checked"</c:if>/>Not
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<input type="submit" value="Ok"/>
				<input type="submit" name="clean" value="Clean"/>
			</td>
		</tr>
	</table>
</form>

<jbfilter:doFilter
	filterId="myIntroFilterHib"
	sessionFactory="${hibernateSessionFactoryRetriever.hibernateSessionFactory}"
	className="org.jbfilter.test.beans.Composer"
	varFilteredItems="filteredComposers"/>

<table border="1">
	<thead>
		<tr>
			<td>id</td>
			<td>Name</td>
			<td>born</td>
			<td>died</td>
			<td>country</td>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${filteredComposers}" var="composer">
			<tr>
				<td>${composer.id}</td>
				<td>${composer.name}</td>
				<td><fmt:formatDate value="${composer.born}" pattern="${datePattern1}"/></td>
				<td><c:if test="${empty composer.died}">&nbsp;</c:if><fmt:formatDate value="${composer.died}" pattern="${datePattern1}"/></td>
				<td>${composer.country.displayName}</td>
			</tr>
		</c:forEach>
	</tbody>
</table>
</body>

</html>